/*
 * Copyright 2016-2021 Pnoker. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xj.business.simulate.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ie.api.center.manager.feign.DeviceClient;
import com.ie.api.center.manager.feign.DriverClient;
import com.ie.api.center.manager.feign.PointClient;
import com.ie.api.center.manager.feign.TenantClient;
import com.xj.business.simulate.bean.model.ModelContent;
import com.xj.business.simulate.bean.model.ModelData;
import com.xj.business.simulate.service.ModelScheduleService;
import com.xj.business.simulate.service.SimulateScheduleService;
import com.xj.business.simulate.service.job.ModelScheduleJob;
import com.xj.business.simulate.service.job.SumlateScheduleJob;
import com.xj.common.model.Device;
import com.xj.common.model.Driver;
import com.xj.common.model.Point;
import com.xj.common.model.Tenant;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型数据初始化，这里设置定时器进行模型固定时间的更新
 */
@Slf4j
@Service
public class SumlateScheduleServiceImpl implements SimulateScheduleService {

    @Resource
    private Scheduler scheduler;


    @Override
    public void initial(String jsonStr) {

        JSONObject jsonObject =JSONObject.parseObject(jsonStr);
        String level = jsonObject.getString("level");//表征是哪个级别的模拟
        Long id = jsonObject.getLong("id");
        String cron = jsonObject.getString("cron"); //"*/5 * * * * ?"  五分钟一次
        Map<String,Object> map = new HashMap<>();
        map.put("level",level);
        map.put("id",id);
        //每五分钟执行一次
        createScheduleJobWithCorn("SimulateScheduleGroup", level+":"+id, cron, SumlateScheduleJob.class,map);

            try {
                if (!scheduler.isShutdown()) {
                    scheduler.start();
                }
            } catch (SchedulerException e) {
                log.error(e.getMessage(), e);
            }

    }


    /**
     * 创建调度任务
     *
     * @param group    group
     * @param name     name
     * @param corn     corn
     * @param jobClass class
     */
    @SneakyThrows
    public void createScheduleJobWithCorn(String group, String name, String corn, Class<? extends Job> jobClass,Map<String,Object> map) {
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(name, group).build();
        jobDetail.getJobDataMap().putAll(map);
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(name, group)
                .startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
                .withSchedule(CronScheduleBuilder.cronSchedule(corn))
                .startNow().build();
        scheduler.scheduleJob(jobDetail, trigger);
    }

}
